﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace StockMatch.WebSite.Mvc
{
    public static class HtmlExtensions
    {
        private static RouteValueDictionary AnonymousObjectToDictionary(object properties)
        {
            if (properties is RouteValueDictionary)
            {
                return properties as RouteValueDictionary;
            }
            if (properties is IEnumerable<KeyValuePair<string, object>>)
            {
                var routeValues = new RouteValueDictionary();
                foreach (KeyValuePair<string, object> keyValuePair in (IEnumerable<KeyValuePair<string, object>>)properties)
                {
                    routeValues.Add(keyValuePair.Key, keyValuePair.Value);
                }
                return routeValues;
            }
            RouteValueDictionary dictionary = new RouteValueDictionary();
            if (properties != null)
            {
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(properties))
                {
                    dictionary.Add(descriptor.Name, descriptor.GetValue(properties));
                }
            }
            return dictionary;
        }

        #region TextEdit
        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">name</param>
        /// <param name="value">value</param>
        /// <param name="verification">验证设置</param>
        /// <param name="message">提示消息设置</param>
        /// <returns></returns>
        public static HtmlString TextEdit(this HtmlHelper htmlHelper, string name, string value, string defaultValue, object verification, object message)
        {
            return htmlHelper.TextEdit(name, value,defaultValue, new { Class = "ui-input" }, verification, message);
        }
        public static HtmlString TextEdit(this HtmlHelper htmlHelper, string name, string defaultValue, object verification, object message)
        {
            return htmlHelper.TextEdit(name, "", defaultValue, new { Class = "ui-input" }, verification, message);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">name</param>
        /// <param name="verification">严重设置</param>
        /// <param name="message">消息提示</param>
        /// <returns></returns>
        public static HtmlString TextEdit(this HtmlHelper htmlHelper, string name, object verification, object message)
        {
            return htmlHelper.TextEdit(name, "","", new { Class = "ui-input" }, verification, message);
        }
        
        public static HtmlString TextEdit(this HtmlHelper htmlHelper, string name, string value, string defaultValue, object param, object verification, object message)
        {
            return htmlHelper.TextEdit(name, value, defaultValue, "text", param, verification, message);
        }
        
        public static HtmlString TextEditPassword(this HtmlHelper htmlHelper, string name, string defaultValue,object verification, object message)
        {
            return htmlHelper.TextEdit(name, "", defaultValue, "password", new { Class = "ui-input" }, verification, message);
        }
        public static HtmlString TextEditPassword(this HtmlHelper htmlHelper, string name, string defaultValue, object param, object verification, object message)
        {
            return htmlHelper.TextEdit(name, "", defaultValue, "password", param, verification, message);
        }
        public static HtmlString TextEditPassword(this HtmlHelper htmlHelper, string name, string value, string defaultValue, object param, object verification, object message)
        {
            return htmlHelper.TextEdit(name, value, defaultValue, "password", param, verification, message);
        }

        /// <summary>
        /// text 文本输入框扩展
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">name</param>
        /// <param name="value">value</param>
        /// <param name="param">属性</param>
        /// <param name="verification">验证设置</param>
        /// <param name="message">信息设置</param>
        /// <returns></returns>
        public static HtmlString TextEdit(this HtmlHelper htmlHelper, string name, string value,string defaultValue,string type, object param, object verification,object message)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<input type=\"{1}\" name=\"{0}\" id=\"{0}\"",name,type);
            if (!string.IsNullOrEmpty(value))
            {
                sb.AppendFormat(" value =\"{0}\"",value);
            }
            if (!string.IsNullOrEmpty(defaultValue))
            {
                sb.AppendFormat(" value =\"{0}\"", defaultValue);
                sb.AppendFormat(" defaultvalue =\"{0}\"", defaultValue);
            }
            if (param != null)
            {
                IDictionary<string, object> dic = AnonymousObjectToDictionary(param);
                foreach (var d in dic) {
                    sb.AppendFormat(" {0}=\"{1}\"",d.Key,d.Value);
                }
            }
            sb.Append("/>");
            if (message == null)
            {
                sb.AppendFormat("<span for=\"{0}\"></span>", name);
            }
            else
            {
                IDictionary<string, object> dic = AnonymousObjectToDictionary(message);
                if (dic["helper"] != null)
                {
                    sb.AppendFormat("<span for=\"{0}\" class=\"nomal\">{1}</span>", name, dic["helper"]);
                }
                sb.AppendFormat("<message for=\"{0}\" ", name);
                foreach (var d in dic)
                {
                    sb.AppendFormat(" {0}=\"{1}\"", d.Key, d.Value);
                }
                sb.Append("></message>");
            }
            sb.AppendFormat("<acronym for=\"{0}\">", name);
            if (verification != null) {
                IDictionary<string, object> dic = AnonymousObjectToDictionary(verification);
                foreach (var d in dic)
                {
                    sb.AppendFormat("<{0}>{1}</{0}>",d.Key,d.Value);
                }
            }
            sb.Append("</acronym>");
            
            return new HtmlString(sb.ToString());
        }
        #endregion
    }
}
